/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.Locatie;
import model.Masterclass;

/**
 *
 * @author daveronvanderham
 */
public class MasterclassToevoegen extends javax.swing.JFrame {

    /**
     * Creates new form MasterclassToevoegen
     */
    private Masterclass clsMasterclass;

    public MasterclassToevoegen() {
        initComponents();

        this.clsMasterclass = new Masterclass();

        txtDatum.setText(clsMasterclass.getDatum());
        txtTijd.setText(clsMasterclass.getTijd());
        txtRating.setText("" + clsMasterclass.getMin_rating());
        txtDeelnemers.setText("" + clsMasterclass.getMin_deelnemers());
        txtInleggeld.setText("" + clsMasterclass.getInleggeld());
        txtPostcode.setText(clsMasterclass.getLocatie_postcode());
        txtHuisnummer.setText(clsMasterclass.getLocatie_huisnummer());

        vulCboMaster();
        vulTabelLocatie();
    }

    private void vulTabelLocatie() {
        //Ophalen van de toernooien uit de database

        String[] kolommen = {"Naam", "Plaats", "Straat", "Postcode", "Huisnummer"};
        DefaultTableModel model = new DefaultTableModel(kolommen, 0);

        try {
            String sql = "SELECT * FROM locatie";
            ResultSet resultaat = Database.executeSelectQuery(sql);

            while (resultaat.next()) {
                String huisnummer = resultaat.getString("locatie_huisnummer");
                String Postcode = resultaat.getString("locatie_postcode");

                Locatie tabelVulling = new Locatie(huisnummer, Postcode);
                model.addRow(tabelVulling.getInformatie());
            }
        } catch (SQLException e) {
        }

        locatieTable.setModel(model);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtDatum = new javax.swing.JTextField();
        txtRating = new javax.swing.JTextField();
        txtDeelnemers = new javax.swing.JTextField();
        cboMaster = new javax.swing.JComboBox();
        txtInleggeld = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        locatieTable = new javax.swing.JTable();
        btnRegistreer = new javax.swing.JButton();
        txtTijd = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtPostcode = new javax.swing.JTextField();
        txtHuisnummer = new javax.swing.JTextField();
        lblPostcode = new javax.swing.JLabel();
        lblHuisnummer = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtDatum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDatumActionPerformed(evt);
            }
        });

        jLabel2.setText("master");

        jLabel3.setText("datum");

        jLabel4.setText("minimale rating");

        jLabel5.setText("minnimale deelnemers");

        jLabel6.setText("inleggeld");

        locatieTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "naam", "plaats", "straat", "huisnummer", "postcode", "max_ deelnemers"
            }
        ));
        locatieTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                locatieTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(locatieTable);
        locatieTable.getColumnModel().getColumn(2).setResizable(false);
        locatieTable.getColumnModel().getColumn(4).setResizable(false);

        btnRegistreer.setText("registreer");
        btnRegistreer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistreerActionPerformed(evt);
            }
        });

        jLabel8.setText("tijd");

        txtPostcode.setFocusable(false);

        txtHuisnummer.setFocusable(false);

        lblPostcode.setText("Postcode");

        lblHuisnummer.setText("Huisnummer");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(15, 15, 15)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jLabel3)
                    .add(jLabel4)
                    .add(jLabel5)
                    .add(jLabel6)
                    .add(jLabel8)
                    .add(lblPostcode)
                    .add(lblHuisnummer))
                .add(21, 21, 21)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(txtPostcode)
                    .add(cboMaster, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtRating)
                    .add(txtDeelnemers)
                    .add(txtInleggeld)
                    .add(txtDatum)
                    .add(txtTijd)
                    .add(txtHuisnummer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(16, 16, 16)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(btnRegistreer)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(15, 15, 15)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cboMaster, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtTijd, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtDatum, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtRating, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtDeelnemers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtInleggeld, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel6))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtPostcode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblPostcode))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtHuisnummer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(lblHuisnummer)))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnRegistreer)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistreerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistreerActionPerformed

        clsMasterclass.setDatum(txtDatum.getText());
        clsMasterclass.setTijd(txtTijd.getText());
        clsMasterclass.setMin_rating(txtRating.getText());
        clsMasterclass.setMin_deelnemers(txtDeelnemers.getText());
        clsMasterclass.setInleggeld(txtInleggeld.getText());
        clsMasterclass.setLocatie_postcode(txtPostcode.getText());
        clsMasterclass.setLocatie_huisnummer(txtHuisnummer.getText());
        clsMasterclass.setMaster("" + cboMaster.getSelectedItem());
        clsMasterclass.setStatus("Actief");

        clsMasterclass.toevoegen();

        if (clsMasterclass.isGevalideerd()) {
            this.dispose();
        } else {
            String bericht = "";
            ArrayList berichten = clsMasterclass.getBericht();
            for (int i = 0; i < berichten.size(); i++) {
                bericht = bericht + (String) berichten.get(i) + "\n";
            }

            JOptionPane.showMessageDialog(null, bericht);
            clsMasterclass.resetBericht();
        }



    }//GEN-LAST:event_btnRegistreerActionPerformed

    private void txtDatumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDatumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDatumActionPerformed

    private void locatieTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_locatieTableMouseClicked
        // TODO add your handling code here:
        int row = locatieTable.getSelectedRow();
        txtHuisnummer.setText((String) locatieTable.getModel().getValueAt(row, 3));
        txtPostcode.setText((String) locatieTable.getModel().getValueAt(row, 4));

    }//GEN-LAST:event_locatieTableMouseClicked

    private void vulCboMaster() {

        try {
            String query = "SELECT * FROM master";
            ResultSet rs = Database.executeSelectQuery(query);

            while (rs.next()) {

                String naam = rs.getString("master_bijnaam");
                cboMaster.addItem(naam);


            }
        } catch (Exception e) {
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MasterclassToevoegen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegistreer;
    private javax.swing.JComboBox cboMaster;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblHuisnummer;
    private javax.swing.JLabel lblPostcode;
    private javax.swing.JTable locatieTable;
    private javax.swing.JTextField txtDatum;
    private javax.swing.JTextField txtDeelnemers;
    private javax.swing.JTextField txtHuisnummer;
    private javax.swing.JTextField txtInleggeld;
    private javax.swing.JTextField txtPostcode;
    private javax.swing.JTextField txtRating;
    private javax.swing.JTextField txtTijd;
    // End of variables declaration//GEN-END:variables
}
